package com.example.cmh.controller;

import com.example.cmh.common.PageResult;
import com.example.cmh.common.Result;
import com.example.cmh.pojo.News;
import com.example.cmh.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @PostMapping("/add")
    public Result<String> addNews(@RequestBody News news, @RequestParam(name = "currentUserId") Integer currentUserId) {
        return newsService.addNews(news, currentUserId);
    }

    @PutMapping("/update/{isAdmin}")
    public Result<String> updateNews(@PathVariable Boolean isAdmin,@RequestBody News news) {
        return newsService.updateNews(isAdmin,news);
    }

    @DeleteMapping("/delete")
    public Result<String> deleteNews(@RequestParam(name = "newsId") Integer newsId) {
        return newsService.deleteNews(newsId);
    }

    @GetMapping("/search")
    public Result<List<News>> searchNews(
            @RequestParam(name = "newsTitle", required = false) String newsTitle,
            @RequestParam(name = "author", required = false) String author,
            @RequestParam(name = "newsSummary", required = false) String newsSummary,
            @RequestParam(name = "newsImagePath", required = false) String newsImagePath,
            @RequestParam(name = "currentUserId") Integer currentUserId) {
        return newsService.searchNews(newsTitle, author, newsSummary, newsImagePath, currentUserId);
    }

    @GetMapping("/detail")
    public Result<News> getNewsDetail(@RequestParam(name = "newsId") Integer newsId) {
        return newsService.getNewsDetail(newsId);
    }
    
    @GetMapping("/list-page")
    public Result<PageResult<News>> getNewsListWithPage(
            @RequestParam(name = "newsTitle", required = false) String newsTitle,
            @RequestParam(name = "author", required = false) String author,
            @RequestParam(name = "currentUserId") Integer currentUserId,
            @RequestParam(name = "newsSummary", required = false) String newsSummary,
            @RequestParam(name = "current", defaultValue = "1") Integer current) {
        return newsService.getNewsListWithPage(newsTitle, author, newsSummary,currentUserId, current);
    }
    
    @PostMapping("/review")
    public Result<String> reviewNews(@RequestParam(name = "newsId") Integer newsId, @RequestParam(name = "status") Integer status) {
        return newsService.reviewNews(newsId, status);
    }
} 